package com.lbb.mvc.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDialog
import com.lbb.mvc.databinding.DialogCenterLoadingBinding
import com.lbb.utils.DisplayUtil

/**
 * 顶部显示的对话框 距离顶部66dp toolbar 最好设置50dp
 */
class LoadingCenterDialog(context: Context?) : AppCompatDialog(context) {
    private var binding: DialogCenterLoadingBinding? = null
    var title: String? = "加载中"
        set(value) {
            field = value
            title?.let {
                binding?.textView?.text = title
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogCenterLoadingBinding.inflate(layoutInflater)
        binding?.let {
            setContentView(it.root)
            it.textView.text = title
            window?.let { item ->
                val drawable = GradientDrawable()
                drawable.cornerRadius = DisplayUtil.dp2px(6f).toFloat()
                drawable.setColor(Color.parseColor("#90000000"))
                it.root.background = drawable
                val params = item.attributes
                params.dimAmount = 0.5f
                params.gravity = Gravity.CENTER
                params.width = ViewGroup.LayoutParams.WRAP_CONTENT
                item.attributes = params
                item.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                val dp16 = DisplayUtil.dp2px(16f)
                val dp66 = DisplayUtil.dp2px(66f)
                item.decorView.setPadding(dp16, dp66, dp16, dp16)

            }
        }


    }
}